package com.orangelife.webservice.service.propFeeReductionInfo.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Response;

import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.propFeeReductionDetail.IPropFeeReductionDetailService;
import com.orangelife.service.propFeeReductionInfo.IPropFeeReductionInfoService;
import com.orangelife.service.propFeeReductionInfo.model.PropFeeReductionInfoDto;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.utils.DateUtil;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.PropertyUtils;
import com.orangelife.utils.StringUtils;
import com.orangelife.webservice.base.BaseService;
import com.orangelife.webservice.base.PageUtils;
import com.orangelife.webservice.service.propFeeReductionInfo.IPropFeeReductionInfoWebService;
import com.orangelife.webservice.service.propFeeReductionInfo.entity.ReductionInfoDto;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation 2015. All Rights Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * propFeeReductionInfo类 的WebService基本作业
 * 
 * @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2016-11-24 14:19:57
 * ==============================================================
 */
@Service("webservice_propFeeReductionInfo")
public class PropFeeReductionInfoWebServiceImpl extends BaseService implements
		IPropFeeReductionInfoWebService {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Resource(name = "propFeeReductionInfoService")
	private IPropFeeReductionInfoService propFeeReductionInfoService;

	@Resource(name = "propFeeReductionDetailService")
	private IPropFeeReductionDetailService propFeeReductionDetailService;

	@Override
	public Response addPropFeeReductionInfo(HttpServletRequest servletRequest,
			ReductionInfoDto reductionInfoDto) throws Exception {

		/************************************************
		 * 检查session
		 ************************************************/
		PageUtils pageUtils = this.validateLogin(servletRequest);

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}

		if (StringUtils.isBlank(reductionInfoDto.getCommID())) {

			return this.abnormalResp(CodeConst.SYS_CODE_1001, "小区信息不能为空");

		}
		if (StringUtils.isBlank(reductionInfoDto.getApartmentID())) {

			return this.abnormalResp(CodeConst.SYS_CODE_1001, "房屋信息不能为空");

		}
		if (reductionInfoDto.getReductionMoney() == null
				|| reductionInfoDto.getReductionMoney().compareTo(
						BigDecimal.ZERO) <= 0) {

			return this.abnormalResp(CodeConst.SYS_CODE_1001, "请输入正确的金额");

		}
		if (StringUtils.isBlank(reductionInfoDto.getReductionReason())) {

			return this.abnormalResp(CodeConst.SYS_CODE_1001, "审批编号不能为空");

		}
		if (StringUtils.isBlank(reductionInfoDto.getApprovalCode())) {

			return this.abnormalResp(CodeConst.SYS_CODE_1001, "房屋信息不能为空");
		}

		if (StringUtils.isBlank(reductionInfoDto.getEffectType())) {

			return this.abnormalResp(CodeConst.SYS_CODE_1001, "生效日期类型不能为空");
		}

		if (reductionInfoDto.getReductionDetail() == null
				|| reductionInfoDto.getReductionDetail().size() == 0) {
			return this.abnormalResp(CodeConst.SYS_CODE_1001, "折减信息不能为空");

		}

		if ("1".equals(reductionInfoDto.getEffectType())) {

			// DateUtil.get

			reductionInfoDto.setEffectTime(DateUtil.getDateByCurDay(new Date(),
					1, 0));
		} else if ("2".equals(reductionInfoDto.getEffectType())) {

			reductionInfoDto.setEffectTime(DateUtil.getDateByCurMon(new Date(),
					1, 0));

		} else if ("3".equals(reductionInfoDto.getEffectType())) {

			if (reductionInfoDto.getEffectTime() == null) {

				return this
						.abnormalResp(CodeConst.SYS_CODE_1001, "自定义生效日期不能为空");

			}

			if (reductionInfoDto.getEffectTime().getTime() < DateUtil
					.getDateByCurDay(new Date(), 1, 0).getTime()) {

				return this.abnormalResp(CodeConst.SYS_CODE_1001,
						"自定义日期仅限设置次日起日期");

			}

		} else {

		}
		reductionInfoDto.setCreateuser(!StringUtils.isBlank(pageUtils
				.getBaseSession().getTrueName()) ? pageUtils.getBaseSession()
				.getTrueName() : pageUtils.getBaseSession().getLoginName());
		reductionInfoDto
				.setCreateuserID(pageUtils.getBaseSession().getUserID());
		reductionInfoDto.setReductionID(IdUtil.getuuId());
		int result = propFeeReductionInfoService
				.addPropFeeReductionInfoAndDetail(reductionInfoDto);
		if (!(result > 0)) {
			return this.abnormalResp(CodeConst.BIS_CODE_13006,
					InfoConst.BIS_INFO_13006);
		}

		return this.normalResp();
	}

	@Override
	public Response editPropFeeReductionInfo(HttpServletRequest servletRequest,
			PropFeeReductionInfoDto propFeeReductionInfo) throws Exception {
		/************************************************
		 * 检查session
		 ************************************************/
		PageUtils pageUtils = this.validateLogin(servletRequest);

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}
		/************************************************
		 * 输入参数ID检查
		 ************************************************/
		Response error = this.validateUUId(
				propFeeReductionInfo.getReductionID(), "reductionID");
		if (error != null) {
			return error;
		}

		/************************************************
		 * 输入参数检查
		 ************************************************/
		// TODO;

		propFeeReductionInfo.setEdituserID(pageUtils.getBaseSession()
				.getUserID());
		int result = propFeeReductionInfoService
				.editPropFeeReductionInfo(propFeeReductionInfo);
		if (!(result > 0)) {
			return this.abnormalResp(CodeConst.BIS_CODE_13006,
					InfoConst.BIS_INFO_13006);
		}
		logger.info(pageUtils.getBaseSession().getLoginName() + "在"
				+ pageUtils.getBaseSession().getLoginType() + "端" + new Date()
				+ "时" + "修改实体成功");

		return this.normalResp();
	}

	@Override
	public Response deletePropFeeReductionInfo(
			HttpServletRequest servletRequest, String reductionID)
			throws Exception {
		/************************************************
		 * 检查session
		 ************************************************/
		PageUtils pageUtils = this.validateLogin(servletRequest);

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}
		/************************************************
		 * 输入参数ID检查
		 ************************************************/
		Response error = this.validateUUId(reductionID, "reductionID");
		if (error != null) {
			return error;
		}
		int result = propFeeReductionInfoService
				.deletePropFeeReductionInfoByLogic(reductionID);
		if (!(result > 0)) {
			return this.abnormalResp(CodeConst.BIS_CODE_13006,
					InfoConst.BIS_INFO_13006);
		}
		logger.info(pageUtils.getBaseSession().getLoginName() + "在"
				+ pageUtils.getBaseSession().getLoginType() + "端" + new Date()
				+ "时" + "删除实体成功");

		return this.normalResp();
	}

	@Override
	public Response findPropFeeReductionInfoByID(
			HttpServletRequest servletRequest, String reductionID)
			throws Exception {
		Map<String, Object> maps = new HashMap<String, Object>();
		/************************************************
		 * 检查session
		 ************************************************/
		PageUtils pageUtils = this.validateLogin(servletRequest);

		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}

		try {

			/************************************************
			 * 输入参数ID检查
			 ************************************************/
			Response error = this.validateUUId(reductionID, "reductionID");
			if (error != null) {
				return error;
			}
			PropFeeReductionInfoDto propFeeReductionInfo = propFeeReductionInfoService
					.findPropFeeReductionInfoByID(reductionID);

			maps.put("errCode", "0");
			maps.put("errInfo", "ok");
			maps.put("entity", propFeeReductionInfo);
			maps.put("pageEntity", PropertyUtils
					.turnToEntity(propFeeReductionDetailService.findReDetail(
							reductionID, null)));

			logger.info(pageUtils.getBaseSession().getLoginName() + "在"
					+ pageUtils.getBaseSession().getLoginType() + "端"
					+ new Date() + "时" + "查询实体成功");
		} catch (AppBizException e) {
			maps.put("errCode", e.getCode());
			maps.put("errInfo", e.getMessage());
		}
		return Response.ok(maps).build();
	}

	@Override
	public Response getAllPropFeeReductionInfo(
			HttpServletRequest servletRequest, String commID, String status,
			String startTime, String endTime, String searchType, String search,
			String pageIndex, String pageSize) throws Exception {

		PageUtils pageUtils = this.validatePageAndSetPage(servletRequest,
				pageIndex, pageSize);
		if (pageUtils.getResponse() != null) {
			return pageUtils.getResponse();
		}
		/*
		 * if (!StringUtils.isBlank(endTime)) { StringUtils.isTime(endTime);
		 * endTime = String.valueOf(Long.parseLong(endTime) / 1000); } if
		 * (!StringUtils.isBlank(startTime)) { StringUtils.isTime(startTime);
		 * startTime = String.valueOf(Long.parseLong(startTime) / 1000); }
		 */

		if (StringUtils.isBlank(commID)) {

			return this.abnormalResp(CodeConst.SYS_CODE_1001, "小区信息不能为空");

		}

		/************************************************
		 * 查询列表
		 ************************************************/

		/*
		 * Map<String, Object> param = new HashMap<String, Object>();
		 * param.put("commID", commID); param.put("startTime", startTime);
		 * param.put("endTime", endTime); param.put("approvalCode",
		 * approvalCode); param.put("status", status); param.put("ownerName",
		 * ownerName); param.put("apartmentNum", apartmentNum);
		 */

		List<Map<String, Object>> list = propFeeReductionInfoService
				.findPropFeeReductionInfoByPage(commID, searchType, startTime,
						endTime, status, null, search,
						pageUtils.getiPageIndex(), pageUtils.getiPageSize());

		/************************************************
		 * 返回结果设定
		 ************************************************/
		Long total = ((Page<Map<String, Object>>) list).getTotal();

		logger.info(pageUtils.getBaseSession().getLoginName() + "在"
				+ pageUtils.getBaseSession().getLoginName() + new Date()
				+ "查询实体成功");

		return this.normalResp(pageUtils.getiPageIndex(),
				pageUtils.getiPageSize(), total,
				PropertyUtils.turnToEntity(list));
	}

	@Override
	public Response bauthFindPropFeeReductionInfo(
			HttpServletRequest servletRequest, List<String> propFeeReductionInfo)
			throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			/************************************************
			 * 输入参数ID检查
			 ************************************************/
			if (propFeeReductionInfo == null
					|| propFeeReductionInfo.size() <= 0) {
				logger.error(InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
				return this.abnormalResp(CodeConst.SYS_CODE_1001,
						InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
			}

			map.put("pageEntity", propFeeReductionInfoService
					.bauthfindPropFeeReductionInfo(propFeeReductionInfo));
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response bauthFindPropFeeReductionInfos(
			HttpServletRequest servletRequest,
			Map<String, Object> propFeeReductionInfo) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		/************************************************
		 * 根据批量查询
		 ************************************************/
		try {

			/************************************************
			 * 输入参数ID检查
			 ************************************************/
			if (propFeeReductionInfo == null
					|| propFeeReductionInfo.size() <= 0) {
				logger.error(InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
				return this.abnormalResp(CodeConst.SYS_CODE_1001,
						InfoConst.SYS_INFO_1001, "亲，请输入相关的参数");
			}

			map.put("pageEntity",
					PropertyUtils.turnToEntity(propFeeReductionInfoService
							.bauthfindPropFeeReductionInfoByMap(propFeeReductionInfo)));
			map.put("errInfo", "ok");
			map.put("errCode", "0");
		} catch (AppBizException e) {
			map.put("errInfo", e.getMessage());
			map.put("errCode", e.getCode());
		}

		return Response.ok(map).build();

	}

	@Override
	public Response clearCache(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		propFeeReductionInfoService.clearCache();
		return this.normalResp();
	}

	@Override
	public Response autoReduction(HttpServletRequest request) throws Exception {

		propFeeReductionInfoService.autoReduction();

		// TODO Auto-generated method stub
		return this.normalResp();
	}

}
